using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;

namespace OA.BLL
{
    public class Authority : Base
    {
        private DAL.Authority dal = null;

        public Authority()
        {
            isession = DB_help.NHibernateHelper.GetCurrentSession(DefaultDataBase);
            dal = new DAL.Authority(isession);
        }

        /// <summary>
        /// 获取站点权限
        /// </summary>
        public Model.Authority GetModelByWebSite(Common.EnumType.RoleUserType UserType, string RoleUserCode, string WebSiteCode)
        {
            return dal.GetModelByWebSite(UserType, RoleUserCode, WebSiteCode);
        }

        /// <summary>
        /// 获取菜单权限
        /// </summary>
        public Model.Authority GetModelByMenu(Common.EnumType.RoleUserType UserType, string RoleUserCode, string WebSiteCode, string MenuCode)
        {
            return dal.GetModelByMenu(UserType, RoleUserCode, WebSiteCode, MenuCode);
        }

        /// <summary>
        /// 获取按钮权限
        /// </summary>
        public Model.Authority GetModelByButton(Common.EnumType.RoleUserType UserType, string RoleUserCode, string WebSiteCode, string MenuCode, string ButtonCode)
        {
            return dal.GetModelByButton(UserType, RoleUserCode, WebSiteCode, MenuCode, ButtonCode);
        }


        public Common.Result UpdateProperty(Common.EnumType.RoleUserType userType, string userCode, string webSiteCode,string menuCode,string buttonCode, string propertyName, bool value)
        {
            Common.Result ret = new Common.Result(false, 0, "修改失败");
            Model.Authority model= null;
            Common.EnumType.ResourceType resourceType;
            if (!string.IsNullOrEmpty(buttonCode))
            {
                model = dal.GetModelByButton(userType, userCode, webSiteCode, menuCode, buttonCode);
                resourceType = Common.EnumType.ResourceType.Button;
            }
            else if (!string.IsNullOrEmpty(menuCode))
            {
                model = dal.GetModelByMenu(userType, userCode, webSiteCode, menuCode);
                resourceType = Common.EnumType.ResourceType.Menu;
            }
            else
            {
                model = dal.GetModelByWebSite(userType, userCode, webSiteCode);
                resourceType = Common.EnumType.ResourceType.WebSite;
            }
            if (model == null)
            {
                model = new Model.Authority()
                {
                    UserType = userType,
                    RoleUserCode = userCode,
                    WebSiteCode = webSiteCode,
                    MenuCode = menuCode,
                    ButtonCode = buttonCode,
                    ResourceType = resourceType
                };
                if (dal.Add(model).success == false)
                {
                    return ret;
                }
            }
            isession.Refresh(LoginUser.LoginManager);
            if (LoginUser.LoginManager.ManagerRoles.Where(r => r.IsSuper).Count() == 0)  //没有 [超级管理员] 角色
            {
                var authlist = dal.GetUserAllAuthority(resourceType,webSiteCode, menuCode, buttonCode, LoginUser.LoginManager);
                if (authlist.All(r => r.IsDelegate == false))
                {
                    ret.message = "权限不足";
                    return ret;
                }
            }
            var propertyType=model.GetType().GetProperty(propertyName);
            if (propertyType == null)
            {
                ret.message = "属性不存在";
                return ret;
            }

            if (Common.ObjectUtils.SetPropertyValue(model, propertyName, value)==false)
            {
                ret.message = "修改失败";
                return ret;
            }

            ret = dal.Update(model);
            return ret;
        }

        /// <summary>
        /// 验证权限
        /// </summary>
        public Common.Result ValidateAuthority(Common.EnumType.ResourceType resourceType, string webSiteCode, string menuCode, string buttonCode, string userName)
        {
            var ret = new Common.Result(false, 0, "权限不足");
            var user = new DAL.Manager(isession).GetModel(userName);
            if (user != null)
            {
                if (user.ManagerRoles!=null && user.ManagerRoles.Where(r => r.IsSuper).Count() > 0)  //有 [超级管理员] 角色
                {
                    ret.success = true;
                    ret.message = "权限验证通过";
                }
                else
                {
                    var authlist = dal.GetUserAllAuthority(resourceType, webSiteCode, menuCode, buttonCode, user);
                    if (authlist.Any(r => r.IsAllow == true) && authlist.All(r => r.IsDeny == false))
                    {
                        ret.success = true;
                        ret.message = "权限验证通过";
                    }
                }
            }
            return ret;
        }


        /// <summary>
        /// 获取用户拥有权限的站点
        /// </summary>
        public IList<Model.WebSite> GetUserWebSite(string userName)
        {
            var sitedal = new DAL.WebSite(isession);
            var websites = sitedal.Get("IsShow=1", "Sort desc", 0, 0);
            for (var i = 0; i < websites.Count; i++)
            {
                if (ValidateAuthority(Common.EnumType.ResourceType.WebSite, websites[i].Code, null, null, userName).success == false)
                {
                    websites.RemoveAt(i--);
                }
            }
            return websites;
        }

        /// <summary>
        /// 获取用户拥有权限的菜单
        /// </summary>
        public IList<Model.Menu> GetUserMenu(string userName,string webSiteCode,string parentMenuCode)
        {
            var menudal = new DAL.Menu(isession);
            var menus=menudal.GetByParent(webSiteCode, parentMenuCode, true);
            for (var i = 0; i < menus.Count; i++)
            {
                if (ValidateAuthority(Common.EnumType.ResourceType.Menu, webSiteCode, menus[i].Code, null, userName).success == false)
                {
                    menus.RemoveAt(i--);
                }
            }
            return menus;
        }


        /// <summary>
        /// 获取用户拥有权限的按钮
        /// </summary>
        public IList<Model.Button> GetUserButton(string userName, string webSiteCode, string menuCode)
        {
            var buttondal = new DAL.Button(isession);
            var buttons = buttondal.GetByParent(webSiteCode, menuCode);
            for (var i = 0; i < buttons.Count; i++)
            {
                if (ValidateAuthority(Common.EnumType.ResourceType.Button, webSiteCode, menuCode, buttons[i].Code,userName).success == false)
                {
                    buttons.RemoveAt(i--);
                }
            }
            return buttons;
        }
    }
}
